package com.plat.security.entity;

// Generated 2011-3-23 11:09:37 by Hibernate Tools 3.2.2.GA

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "SYS_ROLES")
public class SysRoles extends IdEntity<Long> implements java.io.Serializable {

    private static final long serialVersionUID = 8069033086159154327L;

    @Column(name = "ROLE_ID")
    private String roleId;

    @Column(name = "ROLE_NAME")
    private String roleName;

    @Column(name = "ROLE_DESC")
    private String roleDesc;

    @Column(name = "ENABLED")
    private Boolean enabled;

    @Column(name = "ISSYS")
    private Boolean issys;

    // 平台中的子系统
    @Column(name = "module")
    private String module;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "pubRoles")
    private Set<SysUsersRoles> sysUsersRoles = new HashSet<SysUsersRoles>(0);

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "sysRoles")
    private Set<SysRolesAuthorities> sysRolesAuthorities = new HashSet<SysRolesAuthorities>(0);

    @Transient
    private String authorityIds;
    
    public SysRoles() {
    }

    public SysRoles(String roleId) {
        this.roleId = roleId;
    }

    public SysRoles(String roleId, String roleName, String roleDesc) {
        this.roleId = roleId;
        this.roleName = roleName;
        this.roleDesc = roleDesc;
    }

    public SysRoles(String roleId, String roleName, String roleDesc, Boolean enabled, Boolean issys, String module) {
        this.roleId = roleId;
        this.roleName = roleName;
        this.roleDesc = roleDesc;
        this.enabled = enabled;
        this.issys = issys;
        this.module = module;
    }

    public SysRoles(String roleId, String roleName, String roleDesc, Boolean enabled, Boolean issys, String module, Set sysUsersRoles, Set sysRolesAuthorities) {
        this.roleId = roleId;
        this.roleName = roleName;
        this.roleDesc = roleDesc;
        this.enabled = enabled;
        this.issys = issys;
        this.module = module;
        this.sysUsersRoles = sysUsersRoles;
        this.sysRolesAuthorities = sysRolesAuthorities;
    }

    public String getRoleId() {
        return this.roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getRoleName() {
        return this.roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleDesc() {
        return this.roleDesc;
    }

    public void setRoleDesc(String roleDesc) {
        this.roleDesc = roleDesc;
    }

    public Boolean getEnabled() {
        return this.enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Boolean getIssys() {
        return this.issys;
    }

    public void setIssys(Boolean issys) {
        this.issys = issys;
    }

    public String getModule() {
        return this.module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public Set getSysUsersRoles() {
        return this.sysUsersRoles;
    }

    public void setSysUsersRoles(Set sysUsersRoles) {
        this.sysUsersRoles = sysUsersRoles;
    }

    public Set getSysRolesAuthorities() {
        return this.sysRolesAuthorities;
    }

    public void setSysRolesAuthorities(Set sysRolesAuthorities) {
        this.sysRolesAuthorities = sysRolesAuthorities;
    }

    /*
     * 若要把持久类的实例放入Set中(尤其表示多值关联时)，或重用脱管实例， 就要重写该持久类的equals和hashCode。
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object other) {

        if (this == other)
            return true;

        if (!(other instanceof SysRoles))
            return false;

        final SysRoles sysRoles = (SysRoles) other;

        if (!sysRoles.getRoleName().equals(getRoleName()))
            return false;

        if (!sysRoles.getRoleDesc().equals(getRoleDesc()))
            return false;

        return true;

    }

    /*
     * 重写hashCode()。
     * 
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {

        int result;

        result = getRoleName().hashCode();

        result = 29 * result + getRoleDesc().hashCode();

        return result;
    }

    
    public String getAuthorityIds() {
        return authorityIds;
    }

    public void setAuthorityIds(String authorityIds) {
        this.authorityIds = authorityIds;
    }

    public static void main(String[] args) {

        SysRoles role1 = new SysRoles("1", "lxb", "ljh");
        SysRoles role2 = new SysRoles("1", "lxb", "ljh");

        /*
         * 经过试验，当不重写equals和hashCode时显示为false; 重写时，显示为true。 这就是为什么重写equals和hashCode的原因，当你希望从hiberate中提取的对象实例中，
         * 若是所有的字段的内容都相同时，就认为这两个对象实例是相同的，此时就需要重写equals和hashCode。 重写equals和hashCode意味着，混杂在不同上下文及Session中的两个实例对象有了确定的语义。
         */
        System.out.println(role1.equals(role2));

        /*
         * 经过试验，当不重写equals和hashCode时显示为false; 重写时，显示为true。
         */
        System.out.println(role1.hashCode() == role2.hashCode());

    }

}
